package converter.core.utils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Snark
 * Date: 31.03.13
 * Time: 16:06
 * To change this template use File | Settings | File Templates.
 */
public class FileUtil {

    public static List<File> getAllFiles(File directory) {
        ArrayList<File> fileList = new ArrayList<File>();
        processDirectory(directory, fileList);
        return fileList;
    }

    public static void processDirectory(File directory, List<File> fileList) {
        File[] files = directory.listFiles(FileFilter.xmlFilter());
        for (File file : files) {
            processFile(file, fileList);
        }
    }

    private static void processFile(File file, List<File> fileList) {
        if (!file.isDirectory()) {
            fileList.add(file);
        } else {
            processDirectory(file, fileList);
        }
    }

    public static String getExtension(File f) {
        String ext = null;
        String s = f.getName();
        int i = s.lastIndexOf('.');
        if (i > 0 &&  i < s.length() - 1) {
            ext = s.substring(i+1).toLowerCase();
        }
        return ext;
    }
}
